package com.qixiaobao.system.service.impl;

import com.qixiaobao.common.utils.file.FileUtils;
import com.qixiaobao.common.utils.uuid.IdUtils;
import com.qixiaobao.common.utils.oss.OssUtil;
import com.qixiaobao.system.domain.FileInfo;
import com.qixiaobao.system.mapper.FileInfoMapper;
import com.qixiaobao.system.service.IFileInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * @Description 文件信息  业务层处理
 * @Author yangxm
 * @Date 2025/6/17 15:36
 */
@Slf4j
@Service
public class FileInfoServiceImpl implements IFileInfoService {


    @Autowired
    private FileInfoMapper fileInfoMapper;

    /**
     * @Description 根据owner查询文件信息
     * @Author yangxm
     * @Date 2025/6/17 15:36
     * @Param [owner]
     * @return java.util.List<com.qixiaobao.nondirect.invoice.domain.domain.FileInfo>
     */
    @Override
    public List<FileInfo> selectFileInfoListByOwner(String owner) {
        return fileInfoMapper.selectFileInfoListByOwner(owner);
    }

    /**
     * @Description 根据owner查询文件url
     * @Author yangxm
     * @Date 2025/6/18 13:26
     * @param owner
     * @return java.lang.String
     */
    @Override
    public String selectUrlByOwner(String owner) {
        return fileInfoMapper.selectUrlByOwner(owner);
    }

    /**
     * @Description 保存文件信息
     * @Author yangxm
     * @Date 2025/6/18 13:26
     * @param file
     * @param owner
     * @param flag
     * @return com.qixiaobao.nondirect.invoice.domain.domain.FileInfo
     */
    @Override
    public FileInfo saveByOss(MultipartFile file, String owner, boolean flag) {
        log.info("文件拥有者：{}",owner);
        if (file == null) {
            throw new IllegalArgumentException("文件为空");
        }
        String fileOrigName = file.getOriginalFilename();
        if (fileOrigName == null) {
            throw new IllegalArgumentException("文件为空");
        }
        if (!fileOrigName.contains(".")) {
            throw new IllegalArgumentException("缺少后缀名");
        }
        long size = file.getSize();
        String uuid = IdUtils.fastSimpleUUID();
        fileOrigName = fileOrigName.substring(fileOrigName.lastIndexOf("."));
        String path = FileUtils.getPath() + uuid + fileOrigName;
        try {
            OssUtil.ins().uploadFile(path, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        String url = OssUtil.ins().getFileUrl(path);
        FileInfo fileInfo;
        fileInfo = new FileInfo();
        fileInfo.setId(uuid);
        fileInfo.setSize(size);
        fileInfo.setPath(path);
        fileInfo.setUrl(url);
        fileInfo.setOwner(owner);
        fileInfoMapper.insertFileInfo(fileInfo);
        log.info("上传文件{}", path);
        return fileInfo;
    }

    /**
     * @Description 保存文件信息
     * @Author yangxm
     * @Date 2025/6/18 13:26
     * @param file
     * @return java.lang.String
     */
    @Override
    public String save(MultipartFile file) throws IOException {
        if (file == null) {
            throw new IllegalArgumentException("文件为空");
        }
        String fileOrigName = file.getOriginalFilename();
        if (fileOrigName == null) {
            throw new IllegalArgumentException("文件为空");
        }
        if (!fileOrigName.contains(".")) {
            // TODO 临时解决办法，后面再继续寻找办法
            if ("application/octet-stream".equals(file.getContentType())) {
                fileOrigName = fileOrigName + ".jpg";
            } else {
                throw new IllegalArgumentException("缺少后缀名");
            }
        }
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        fileOrigName = fileOrigName.substring(fileOrigName.lastIndexOf("."));
        String path = FileUtils.getPath() + uuid + fileOrigName;
        InputStream inputStream=null;
        try {
            inputStream=file.getInputStream();
            OssUtil.ins().uploadFile(path, inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null!=inputStream){
                inputStream.close();
            }
        }
        log.debug("上传文件{}", path);
        return OssUtil.ins().getFileUrl(path);
    }
}
